package javbot;

import java.io.File;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import javbot.gui.ConsolePanel;
import javbot.keydata.KeyData;
import javbot.keydata.KeyDataInputFrame;
import javbot.keydata.KeyDataProps;
import javbot.mail.MailChecker;
import javbot.mail.MailProps;

public class GlobalClass {

	public static KeyData keyData = new KeyData();
	public static MailProps mailProps = new MailProps();
	public static MailChecker mailChecker;
	public static int repeat = -1;
	public static ArrayList<File> files = new ArrayList<File>();
	public static ConsolePanel cPanel;
	
	public static void startMailChecker() {
		String server = GlobalClass.mailProps.getMailServerIn();
		String login = GlobalClass.mailProps.getLogin();
		char[] pass = GlobalClass.mailProps.getPass();
		startMailChecker(server, login, pass);		
	}	
	
	public static void startMailChecker(String server, String login, char[] pass) {
		stopMailChecker();
		
		StringBuffer sb = new StringBuffer(pass.length);
		for (int i = 0; i < pass.length; i++) {
			sb.append(pass[i]);
		}
		
		GlobalClass.mailChecker = new MailChecker(server, login, sb.toString());
		mailChecker.start();
	}
	
	
	public static void stopMailChecker() {
		if (mailChecker != null) {
			mailChecker.interrupt();
			mailChecker = null;
			System.gc();
		}		
	}
	
	
	public static void initKeyData(boolean forceRegenerate) throws Exception {
		if (forceRegenerate || !KeyDataProps.file.exists()) {
						
			KeyDataInputFrame kit = new KeyDataInputFrame();
			
			JOptionPane.showMessageDialog(kit,
		    "Javbot will now obtain a mapping of your keyboard.\n" +
		    "Do not press any keys or move the mouse for a few seconds.\n" +
		    "Press OK to continue...",
		    "Map Keyboard",
		    JOptionPane.WARNING_MESSAGE);			
			
			keyData = kit.getKeyDataFromKeyboard();
			kit.dispose();
			
		} else {
			keyData.keyDataProps.readKeyData();
		}
	}

}
